package Tree;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2024/08/27
 * 二叉树递归遍历
 */
public class RecursiveBinaryTree {
    /**
     * 内部类
     */
    private static class Node {
        Integer data;
        Node left;
        Node right;

        public Node(Integer data) {
            this.data = data;

        }


    }

    public static void main(String[] args) {
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.left = new Node(6);
        root.right.right = new Node(7);


        System.out.println("前序遍历");
        RecursiveBinaryTree.preOrder(root);
        System.out.println("\n"+"中序遍历");
        RecursiveBinaryTree.inOrder(root);
        System.out.println("\n"+"后序遍历");
        RecursiveBinaryTree.postOrder(root);
    }


    /**
     * 递归遍历
     */
    private static void recursive(Node node) {
        if (node == null) {
            return;
        }
        recursive(node.left);
        recursive(node.right);
    }

    /**
     *前序遍历
     */
    private static void preOrder(Node node) {
        if (node == null) {
            return;
        }
        System.out.print(node.data);
        preOrder(node.left);
        preOrder(node.right);
    }

    /**
     * 中序遍历
     */
    private static void inOrder(Node node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.print(node.data);
        inOrder(node.right);
    }


    /**
     * 后序遍历
     */
    private static void postOrder(Node node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.print(node.data);
    }
}
